#!/usr/bin/env python
# encoding: utf-8
"""
banner.py

Created by Manabu Terada on 2009-09-25.
Copyright (c) 2009 CMScom. All rights reserved.
"""
from zope import schema
from Acquisition import aq_inner
from zope.component import getMultiAdapter
from zope.formlib import form
from zope.interface import implements
from plone.app.portlets.portlets import base
from plone.memoize import ram
from plone.portlets.interfaces import IPortletDataProvider
from plone.memoize.compress import xhtml_compress
from plone.memoize.instance import memoize
from plone.app.portlets.cache import render_cachekey
from Products.CMFCore.utils import getToolByName
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from c2.site.icos09 import icos09MessageFactory as _
from plone.memoize.instance import memoize

class IBannerPortlet(IPortletDataProvider):
    """"""
    path = schema.TextLine(title=_(u'Get items from path'),
                       description=_(u'Where is path.'),
                       required=True,
                       default=u'/banner')

    count = schema.Int(title=_(u'Number of items to display'),
                       description=_(u'How many items to list.'),
                       required=True,
                       default=5)

    state = schema.Tuple(title=_(u"Workflow state"),
                         description=_(u"Items in which workflow state to show."),
                         default=('published', ),
                         required=True,
                         value_type=schema.Choice(
                             vocabulary="plone.app.vocabularies.WorkflowStates")
                         )

class Assignment(base.Assignment):
    implements(IBannerPortlet)

    def __init__(self, path='', count=5):
        self.path = path
        self.count = count
        # self.state = state

    @property
    def title(self):
        return _(u"Banner Portlet")

class Renderer(base.Renderer):
    _template = ViewPageTemplateFile('banner.pt')

    @ram.cache(render_cachekey)
    def render(self):
        return xhtml_compress(self._template())

    def navigation_root_url(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                            name=u'plone_portal_state')
        return portal_state.navigation_root_url()
    
    @property
    def available(self):
        return bool(len(self._data()))

    def get_items(self):
        return self._data()

    @memoize
    def _data(self):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')
        portal_state = getMultiAdapter((self.context, self.request), 
                                        name=u'plone_portal_state')
        portal = portal_state.portal()
        banner_path = self.data.path
        if not banner_path.startswith('/'):
            banner_path = '/' + banner_path
        path = '/'.join(portal.getPhysicalPath()) + banner_path
        limit = self.data.count
        # state = self.state
        return catalog(portal_type='Bannar',
                       # review_state=state,
                       path=path,
                       sort_on='getObjPositionInParent',
                       sort_order='ascending',
                       sort_limit=limit)[:limit]

class AddForm(base.AddForm):
    form_fields = form.Fields(IBannerPortlet)

    def create(self, data):
        return Assignment(**data)

class EditForm(base.EditForm):
    form_fields = form.Fields(IBannerPortlet)
